<?php

namespace App\Models;

class Product extends Base
{

    //分页获取列表数据
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where);
            if(!empty($whereDates)){
                $model = $model->whereDate('created_at','>=',$whereDates[0])->whereDate('created_at','<',$whereDates[1]);
            }
            $model=$model->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            foreach ($result['data']['list'] as &$item){
                if($item['cids']){
                    $arr = explode(',',$item['cids']);
                    $names =  (new ProdCategory())->whereIn('id',$arr)->pluck('catename_zn')->toArray();
                    $item['catenames'] = implode(',',$names);
                }
            }
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //新增单个商品
    public function oneInsert($data=array()){
        try {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id = $this->insertGetId($data);
            if($id){
                return [
                    'code'=>0,
                    'data'=>$id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单个商品
    public function doUpdate($data){
        try {
            $now = date('Y-m-d H:i:s');
            $data['updated_at'] = $now;
            $res = $this->where('id',$data['id'])->update($data);
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //改变商品状态
    public function changeStatus($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            if($data['status']){
                $model->deleted_at = null;
            }else{
                $model->deleted_at = date('Y-m-d H:i:s');
            }
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$model->id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'操作失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //设置商品等级折扣
    public function setDiscount($data){
        try {
            $model = new ProdDiscount();
            $result =  $model->where('proid',$data['proid'])->delete();
            if($result===false){
                return [
                    'code'=>1,
                    'message'=>'操作失败,请重试'
                ];
            }else{
                $levs = json_decode($data['discounts'],true);
                $insert = [];
                $now = date('Y-m-d H:i:s');
                foreach ($levs as $key=>$val){
                    $insert[]=['levid'=>$key,'proid'=>$data['proid'],'discount'=>$val,'created_at'=>$now,'updated_at'=>$now];
                }
                $res = $model->insert($insert);
                if($res){
                    return [
                        'code'=>0,
                        'data'=>''
                    ];
                }else{
                    return [
                        'code'=>1,
                        'message'=>'操作失败'
                    ];
                }
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //设置商品图片
    public function setProdImgs($data){
        try {
            $model = new Image();
            $result =  $model->where([
                ['rid','=',$data['proid']],['type','=',1]
            ])->delete();
            if($result===false){
                return [
                    'code'=>1,
                    'message'=>'操作失败,请重试'
                ];
            }else{
                $levs = $data['images'];
                $insert = [];
                $now = date('Y-m-d H:i:s');
                $title = $this->select('proname','proname_zn')->first($data['proid']);
                foreach ($levs as $val){
                    $insert[]=['type'=>1,'rid'=>$data['proid'],'src'=>$val,'title'=>$title->proname,'title_zn'=>$title->proname_zn,'created_at'=>$now,'updated_at'=>$now];
                }
                $res = $model->insert($insert);
                if($res){
                    return [
                        'code'=>0,
                        'data'=>''
                    ];
                }else{
                    return [
                        'code'=>1,
                        'message'=>'操作失败'
                    ];
                }
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //前台商品分页列表
    public function getList($model,$page,$limit,$user,$lang='zn'){
        try{
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->get()->toArray();
            if($user){
                foreach ($result as &$item){
                    $discounts = $this->getProdDiscount($user,$item['id']);
                    $item['rprice'] = $item['rprice'] * $discounts['discount'] / 100;
                }
            }
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return  [
                'code'=>0,
                'data'=>$data
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }
}
